#include "loopstatement.h"
#include <iostream>
using namespace std;

LoopStateMent::LoopStateMent()
{

}

void LoopStateMent::show(){
    cout << endl << endl;
    cout << "分支结构==============================================="<< endl;

    // for循环
    int sum = 0;
    for(int i=0; i<=100; i++){
        sum += i;
    }
    cout << "sum: " << sum << endl;


    // while循环
    int i = 0;
    int sum2 = 0;
    while(i<=100){
        sum2 += i;
        i++;
    }
    cout << "sum2: " << sum2 << endl;

    // do while循环
    i = 0;
    int sum3 = 0;
    do{
        i++;
        sum3 += i;
    }while(i<100);
    cout << "sum3: " << sum3 << endl;

    {
        // 初始化语句可声明多个名字
        cout << endl << endl;
        cout << "初始化语句可声明多个名字===================================="
             << endl;
        for (int i = 0, *p = &i; i < 9; i += 2) {
          cout << i << ':' << *p << ' ';
        }
        cout << '\n';
      }

    {
        // （循环）条件可为声明
        cout << endl << endl;
        cout << "（循环）条件可为声明====================================" << endl;
        char cstr[] = "Hello";  // c语言中声明字符串
        for (int n = 0; char c = cstr[n]; ++n) {
          cout << c;
          cout << '\n';
        }
      }

    {
              // 初始化语句可为表达式
              cout << endl << endl;
              cout << "初始化语句可为表达式====================================" << endl;

              int n = 0;
              for (cout << "循环开始\n"; cout << "循环测试\n";
                   cout << "迭代 " << ++n << '\n') {
                if (n > 1) {
                          break;  //终止循环，所有的循环都不再执行了；
                  continue;  // 终止当前的循环；  继续往后开始下一次循环；
                }
                cout << '\n';
              }
      }

    {
        // 带声明条件的 while 循环
        cout << endl << endl;
        cout << "带声明条件的 while 循环===================================="
             << endl;

        cout << "带声明条件的 while 循环" << endl;  // 有点像C#中foreach遍历
        char cstr[] = "Hello";
        int k = 0;
        while (char c = cstr[k++]) {
          cout << c;
          cout << '\n';
        }
      }

    {  // 输出九九乘法表
        cout << endl << endl;
        cout << "输出九九乘法表====================================" << endl;
        for (int i = 1; i <= 9; i++) {
          for (int j = 1; j <= i; j++) {
            cout << j << "*" << i << "=" << i * j << " ";
          }
          cout << endl;
        }
      }




}
